/* Event.java
 * 
 * Event is a event on a tour, it has a start time,
 * stop time, start place, end place and has a
 * certain activity based on the provider
 *
 * Greg Warner
 * Derek Dupuis
 * Michael Dixey
 * Alex Schwartz
 */

import java.util.*;
import java.text.*;

public class Event
{
   private GregorianCalendar startTime;
   private GregorianCalendar endTime;
   private String startPlace;
   private String endPlace;
   private String eventName;
   private Tour tour;
   private Provider provider;
   private float fitnessLevel;
   
   public Event( String sP, String eP, int sT, int eT, float fL, int day,
                String eN, Tour t, Provider p )
   {
      startPlace = sP;
      endPlace = eP;
      
      //calculates and creates Gregorian Calendars for the 
      startTime = new GregorianCalendar( 0, 0, day, sT / 100, sT % 100 );
      endTime = new GregorianCalendar( 0, 0, day, eT / 100, eT % 100 );
      
      eventName = eN;
      tour = t;
      provider = p;
      fitnessLevel = fL;
   }
   
   //---------------------------accessors------------------------------
   public String startPlace() { return startPlace; }
   
   public String endPlace() { return endPlace; }
   
   public GregorianCalendar startTime() { return startTime; }
   
   public GregorianCalendar endTime() { return endTime; }
   
   public String eventName() { return eventName; }
   
   public Tour tour() { return tour; }
   
   public Provider provider() { return provider; }
   
   public float fitnessLevel() { return fitnessLevel; }
   
   //------------------------------------------------------------------
   
   //returns 0 if conflicting, 1 if this event is after the
   //compared event, -1 if it is before the compared event
   public int compareTo( Event e )
   {
      GregorianCalendar eStart = e.startTime();
      GregorianCalendar eEnd = e.endTime();

      if( startTime.compareTo( eStart ) < 0 &&
         endTime.compareTo( eStart ) <= 0 )
         return -1;
      else if( startTime.compareTo( eEnd ) >= 0 &&
              endTime.compareTo( eEnd ) > 0 )
         return 1;
      else
         return 0;
   }
   
   public boolean providerConflict()
   {
      //get times in integer for easy comparison
      int sTime = startTime.get( Calendar.HOUR_OF_DAY ) * 100 +
         startTime.get( Calendar.MINUTE );
      
      int eTime = endTime.get( Calendar.HOUR_OF_DAY ) * 100 +
         endTime.get( Calendar.MINUTE );
      
      int openTime = provider.openTime().get( Calendar.HOUR_OF_DAY ) * 100 +
         provider.openTime().get( Calendar.MINUTE );
      
      int closeTime = provider.closeTime().get( Calendar.HOUR_OF_DAY ) * 100 +
         provider.closeTime().get( Calendar.MINUTE );
      
      //if starts early than open or end later than close, return true
      return ( sTime < openTime || eTime > closeTime );
   }
   
   //convert event to a string
   public String toString()
   {
      DateFormat tF = DateFormat.getTimeInstance( DateFormat.SHORT );
      String s = "\t";
      s = s + eventName;
      s += provider.service().type().compareTo( "travel" ) == 0 ?
         " to " : " at ";
      s = s + endPlace + " from ";
      s = s + tF.format( startTime.getTime() ) + " to ";
      s = s + tF.format( endTime.getTime() ) + " on day ";
      s = s + endTime.get( Calendar.DAY_OF_MONTH ) + "\n";
      return s;
   }
}